package com.Siven.refreshlistviewSample;

import com.Siven.view.RefreshListView;
import com.Siven.view.RefreshListView.OnRefreshDataLister;

import android.os.Bundle;
import android.os.SystemClock;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

	private RefreshListView	rlv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rlv = (RefreshListView) findViewById(R.id.rlv_listview);

		rlv.setAdapter(new MyAdapter());
		
		//设置头刷新
		refreshPullDownUse();
		
		//设置加载更多数据
		loadingMoreDataUse();

	}

	// 下拉菜单的方法
	private void refreshPullDownUse() {

		rlv.setIsRefresh(true);

		rlv.setOnRefreshListener(new OnRefreshDataLister() {

			@Override
			public void refreshMore() {

			}

			@Override
			public void refreshData() {
				// 添加刷新数据的代码,模拟耗时操作
				new Thread() {

					public void run() {
						SystemClock.sleep(2000);
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								rlv.refreshFinish();

							}
						});
					};
				}.start();
			}
		});

	}

	// 加载更多的方法
	private void loadingMoreDataUse() {
		rlv.setIsLoadingMore(true);
		rlv.setOnRefreshListener(new OnRefreshDataLister() {

			@Override
			public void refreshMore() {
				new Thread() {

					public void run() {
						SystemClock.sleep(2000);
						runOnUiThread(new Runnable() {

							@Override
							public void run() {
								rlv.refreshFinish();

							}
						});
					};
				}.start();

			}

			@Override
			public void refreshData() {

			}
		});
	}

	private class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return 20;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			TextView tv = new TextView(getApplicationContext());
			tv.setGravity(Gravity.CENTER);
			tv.setTextSize(25);
			tv.setText("text" + position);
			return tv;
		}

	}

}
